package com.example.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;
import com.example.entry.RequestHistory;
import com.example.log.Logs;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 呆猫
 * @version 1.0
 * @since 2025年02月20日 10:43
 * ------------🌈-💨------------
 * 美 好 生 活 从 维 护 代 码 开 始
 * ----------------------------
 */
public class HistoryUtils {
	private static final String HISTORY_FILE = "request_history.json";


	public static void saveHistoryToFile(List<RequestHistory> historyList) {
		Logs.getInstance().log("开始保存历史记录到文件");
		try {
			String json = JSONUtil.toJsonStr(historyList);
			FileUtil.writeUtf8String(json, HISTORY_FILE);
		} catch (Exception e) {
			Logs.getInstance().log("保存历史记录到文件失败: " + e.getMessage());
		}
	}



	public static List<RequestHistory>  loadHistoryFromFile() {
		Logs.getInstance().log("开始加载历史记录...");
		List<RequestHistory> requestHistories = new ArrayList<>();
		try {
			File file = new File(HISTORY_FILE);
			if (file.exists()) {
				String json = FileUtil.readUtf8String(file);
				List<RequestHistory> loadedHistory = JSONUtil.toList(json, RequestHistory.class);
				requestHistories.addAll(loadedHistory);
			}
			Logs.getInstance().log("历史记录加载完成");
		} catch (Exception e) {
			Logs.getInstance().log("加载历史记录失败: " + e.getMessage());
		}
		return requestHistories;
	}







}
